<template>
  <div>
    <!-- 三方票号查询 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhInvoiceNumberQuery"
        :isSelectColumn="false"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :rowstyles="rowstyles"
        :onSyncTableData="syncTableData"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls">
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="visible" title="余额锁定明细" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <SonModel :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryList, exportExcel, gettmFiAccountType,exporturl } from '@/api/omFinance/FinanceManage/InvoiceNumberQuery';
import { exportExcelFile } from '@/utils/index';
import SonModel from './SonModel';

export default {
  name: 'InvoiceNumberQuery',
  mixins: [dictionary],

  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      rowstyles: [],
      visible: false,
      exportLoading: false
    };
  },
  components: {
    SonModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    this.gettmFiAccountType();
  },
  methods: {
    detailHandle(row) {
      this.visible = true;
      this.formData = row;
    },
    emitCloseHandler(val, data) {
      this.visible = val;
    },
    syncTableData(data) {
      if (!data.length) return;
      data.forEach(x => {
        if (x.isDeleted == 1) {
          this.rowstyles.push({ row: x, styles: { background: '#909399' } });
        }
      });
    },
    // 资金类型
    async gettmFiAccountType() {
      const res = (await gettmFiAccountType({ fundsType: 0, ownerCode: this.dealerCode })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.topFilterList.find(x => x.fieldName == 'accountType').itemList = res;
    },
    // 导出
    async exportHandle() {
        let json = Object.assign({}, { CODE: 'CODE_00000233' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '三方票号查询');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },

    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '三方票号',
          fieldName: 'billNo',
          placeholder: '请选择'
        },
        {
          type: 'SELECT',
          label: '资金类型',
          fieldName: 'accountType',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'RANGE_DATE',
          label: '记账时间范围',
          fieldName: 'startAccountDate|endAccountDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        },
        {
          type: 'RANGE_DATE',
          label: '到期日期',
          fieldName: 'startExpireDate|endExpireDate',
          dateType: 'exactdaterange',
          placeholder: '请选择'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '经销商代码',
          dataIndex: 'ownerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '记账时间',
          dataIndex: 'accountDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '三方票号',
          dataIndex: 'billNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '票面金额',
          dataIndex: 'billAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },
        {
          title: '可用余额',
          dataIndex: 'availableAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true
        },

        {
          title: '已用金额',
          dataIndex: 'realAmount',
          sorter: true,
          filter: true,
          filterType: 'input',
          precision: 2,
          numberFormat: true,
          render: props => {
            return (
              <el-link type="warning" underline={true} onClick={() => this.detailHandle(props.row)}>
                {props.row.realAmount}
              </el-link>
            );
          }
        },
        {
          title: '出票日期',
          dataIndex: 'billDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '到期日期',
          dataIndex: 'expireDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    }
  }
};
</script>
